/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package za.co.pass.jtorrenteditor.list.model;

import java.util.LinkedList;
import java.util.List;
import javax.swing.ListModel;
import javax.swing.event.ListDataListener;
import za.co.pass.jtorrenteditor.helper.Utils;

/**
 *
 * @author alabuschagne
 */
public class PiecesListModel implements ListModel
{
    private List<short[]> listBytes = new LinkedList<short[]>();

    public int getSize()
    {
        return listBytes.size();
    }

    public short[] getAt(int index)
    {
        return listBytes.get(index);
    }

    public Object getElementAt(int index)
    {
        short[] b = listBytes.get(index);
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < b.length; i++)
        {
            String str = Integer.toHexString(b[i]);
            str = Utils.addLeadingZeros(str, 2);
            str = str.toUpperCase();
            sb.append(str);
            if(i < b.length - 1)
            {
                sb.append(" ");
            }
        }
        return sb.toString();
    }

    public void addListDataListener(ListDataListener l)
    {
    }

    public void removeListDataListener(ListDataListener l)
    {
    }

    public void set(short[] manyBytes)
    {
        double d = (double) manyBytes.length / 20.0;
        //System.out.println(Double.toString(d));
        short[] b = new short[20];
        int count = 0;
        for(int i = 0; i < manyBytes.length; i++)
        {
            b[count] = manyBytes[i];
            count++;
            if(count == 20)
            {
                this.listBytes.add(b);
                count = 0;
                b = new short[20];
            }
        }
    }
}
